/**
 * 
 */
package com.wskj.library.book.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.wskj.library.pojo.BookInfo;
import com.wskj.library.service.BookService;
import com.wskj.library.service.impl.BookServiceImpl;

/**
 * @author 胡洋
 * @describe 
 * @Date 2018年3月26日上午10:11:59
 * @version 1.0
 */
@WebServlet(urlPatterns="/book/exportExcel.do")
public class BookExcelOutServlet extends HttpServlet {
    //图书的服务类......
	private BookService bookService=new BookServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		List<BookInfo> books= bookService.queryAllBooks();
		
		resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
		String filename="图书信息列表.xlsx";
		resp.setHeader("content-disposition","attachment;filename="+URLEncoder.encode(filename,"UTF-8"));  
		ServletOutputStream out = resp.getOutputStream();
		
		Workbook workbook=new XSSFWorkbook();
		Sheet sheet = workbook.createSheet("图书信息列表");
		
		//设置列的宽度 ....
		sheet.setColumnWidth(5, 256*18);
		sheet.setColumnWidth(6, 256*17);
		sheet.setColumnWidth(7, 256*17);
		//设置列时间的样式 ......
		 CellStyle cellStyle = workbook.createCellStyle();
		 cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("yyyy年mm月dd日"));
		    
		
		Row title = sheet.createRow(0);
		
		title.createCell(0).setCellValue("序号");
		title.createCell(1).setCellValue("图书名称");
		title.createCell(2).setCellValue("图书分类");
		title.createCell(3).setCellValue("图书作者");
		title.createCell(4).setCellValue("价格");
		title.createCell(5).setCellValue("出版社");
		title.createCell(6).setCellValue("出版时间");
		title.createCell(7).setCellValue("入馆时间");
		title.createCell(8).setCellValue("借出状态");
		title.createCell(9).setCellValue("图书摘要");
	
		for(int i=1;i<=books.size();i++) {
			BookInfo b=books.get(i-1);
			Row row = sheet.createRow(i);
			row.createCell(0).setCellValue(i);
			row.createCell(1).setCellValue(b.getBname());
			row.createCell(2).setCellValue(b.getCname());
			row.createCell(3).setCellValue(b.getAuthor());
			row.createCell(4).setCellValue(b.getPrice());
			row.createCell(5).setCellValue(b.getPublisher());
			Cell cell6 = row.createCell(6);
			Cell cell7 = row.createCell(7);
			
			cell6.setCellStyle(cellStyle);
			cell6.setCellValue(b.getPublishTime());
			cell7.setCellStyle(cellStyle);
			cell7.setCellValue(b.getCollectDay());
			
			row.createCell(8).setCellValue(b.getState()==0?"可以借出":"已经借出");
			row.createCell(9).setCellValue(b.getSumary());			
		}
		
		//输出流 下载 图书的信息 
		workbook.write(out);
		
		
		out.close();		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}

}
